package com.project.erp.dao.crm.company;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.project.erp.common.dao.HibernateDao;
import com.project.erp.common.utils.Page;
import com.project.erp.model.crm.company.Company;

/**
 * 
 * COPYRIGHT (C) 2013 BY XXX公司

 *  功能模块名：CRM-公司信息管理

 *  功能描述：管理自己公司信息（用于开发票等）DAO类

 * @author LiJiaGuo    

 * @version 1.0  

 * @created 2013-12-8 下午3:18:29
 */
@Repository
public class CompanyDao extends HibernateDao<Company, Long> {
	/**
	 * 根据id列表删除
	 * @param idsList
	 */
	public void delCompanyByCompanyIds(final List<Long> idsList){
		this.deleteByIdsList(idsList);
	}
	
	/**
	 * 根据条件查询
	 * @company page
	 * @company company
	 * @return
	 */
	public Page<Company> getCompanyPageBySearch(Page<Company> page,Company company){
		List<Criterion> list = new ArrayList<Criterion>();
		if(StringUtils.isNotEmpty(company.getExpComName())){
			Criterion expComName = Restrictions.ilike("expComName",company.getExpComName(),MatchMode.ANYWHERE);
			list.add(expComName);
		}
		if(StringUtils.isNotEmpty(company.getCompanyName())){
			Criterion companyName = Restrictions.ilike("companyName",company.getCompanyName(),MatchMode.ANYWHERE);
			list.add(companyName);
		}
		
		page = this.findPage(page,list.toArray(new Criterion[list.size()]));
		
		return page;
	}
	
	/**
	 * 取得默认显示的公司信息
	 * @company company
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Company getDefaultCompany(){
		Criteria c = this.getSession().createCriteria(Company.class);
		//if(company.getId() != null){
			//c.add(Restrictions.ne("id",company.getId()));
		//}
		
		c.add(Restrictions.eq("isDefault",true));
		
		List<Company> companys = c.list();
		Company comp = null;
		
		if((companys != null) && (companys.size() > 0)){
			comp = companys.get(0);
		}
		return comp;
	}
}
